<!-- 页面 -->
<template>
	<view class="ss-user-info-wrap ss-p-t-50 distance">
		<view class="ss-flex  ss-col-center ss-m-b-20  ss-m-t-10" >
			<view class="left-box ss-flex-1 ss-flex ss-col-center  ss-m-l-36">
				<view class="avatar-box ss-m-r-24" @tap="toLogin">
					<image class="avatar-img" :src="
		      isLogin
		        ? sheep.$url.cdn(userInfo.avatar)
		        : sheep.$url.static('/static/img/shop/default_avatar.png')
		    " mode="aspectFill"></image>
				</view>
				<view class="ss-flex-1">
					<view class="nickname-box ss-m-r-10">
						<view class="ss-flex ss-m-t-20" style="flex-wrap:wrap" @click="toLogin">
							<view class="nick-name ss-m-b-10 ss-flex-1 ss-line-1">{{ userInfo?.nickname || nickname }}</view>
							<view class="tagSore-box ss-m-b-10" v-if="isLogin">
							</view>
						</view>
						<view class="ss-flex" v-if="isLogin">
							<view  style="color:#555454" class="font-1">账号：{{userInfo?.mobile}}</view>
							<image style="width:50rpx;height:50rpx;margin-left: 20rpx;"
								:src="sheep.$url.cdn('/static/icon/code.png')" v-if="isAuditMode==false"
								mode="aspectFill" @tap="sheep.$router.go('/pages/user/share')">></image>
						</view>
						
						<view class="ss-flex-row ss-row-between ss-m-t-10 ss-flex-1 po">
							<view class="img-groups">
								<!-- 营销身份   当是股东时-->
								<template v-if="userInfo?.extend?.max_role=='shareholder'">
									<image @click="sheep.$router.go('/pages/user/badge')"
										:src="sheep.$url.cdn(giveaway[userInfo?.extend?.max_role])"
										class="income-img" v-if="isLogin&&userInfo?.extend?.max_role!=='user'&&userInfo?.extend?.max_role!=='platform'"></image>
									<image @click="sheep.$router.go('/pages/user/badge')"
										:src="calcSrc"
										class="income-img" v-if="isLogin&&userInfo?.extend?.max_role!=='user'&&userInfo?.extend?.max_role!=='platform'"></image>	
								</template>
								<!-- 是其他身份时 -->
								<template v-else>
									<image @click="sheep.$router.go('/pages/user/badge')"
										:src="sheep.$url.cdn(giveaway[userInfo?.extend?.max_role])"
										class="income-img" v-if="isLogin&&userInfo?.extend?.max_role!=='user'&&userInfo?.extend?.max_role!=='platform'"></image>
								</template>
								
											 							
								<!-- 会员		 -->
								<image v-if="grade&&storeCard[grade]"
									:src="sheep.$url.cdn(storeCard[grade])"
									class="huiyuan-img"></image>
									
							</view>
							<view class="btn-groups">
								<image :src="sheep.$url.cdn('/static/user/kefu.png')" v-if="isAuditMode==false&&kefuShow==1"
									style="width:50rpx;height:50rpx;margin-right:10rpx;" class="kefu-class" @click="customerClick"></image>
								<image style="width:56rpx;height:56rpx;margin-left: 10rpx;"
									:src="sheep.$url.cdn('/static/icon/setting.png')"
									mode="aspectFill" @tap="sheep.$router.go('/pages/user/setting')"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
<!-- 		<view class="open-vip" v-if="isLogin" @click="sheep.$router.go('/pages/user/vip')">
			 <image class="open-vip-image" :src="sheep.$url.cdn('/static/img/kaihuiyuan.png',ygb)" />
		</view> -->
		<view class="bind-mobile-box ss-flex ss-row-between ss-col-center"
			v-if="isLogin && !userInfo.verification?.mobile">
			<view class="ss-flex">
				<text class="cicon-mobile-o"></text>
				<view class="mobile-title ss-m-l-20"> 点击绑定手机号确保账户安全 </view>
			</view>
			<button class="ss-reset-button bind-btn" @tap="onBind">去绑定</button>
		</view>



	</view>
</template>

<script setup>
	/**
	 * 用户卡片
	 *
	 * @property {Number} leftSpace 									- 容器左间距
	 * @property {Number} rightSpace 									- 容器右间距
	 *
	 * @property {String} avatar 					- 头像
	 * @property {String} nickname 					- 昵称
	 * @property {String} vip		  				- 等级
	 * @property {String} collectNum 				- 收藏数
	 * @property {String} likeNum 					- 点赞数
	 *
	 *
	 */
	import {
		computed,
		reactive,
		ref
	} from 'vue';
	import sheep from '@/sheep';
	import {
		showShareModal,
		showAuthModal,
		showKefuModal
	} from '@/sheep/hooks/useModal';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	const grade = ref(null)
	const storeCard = {
		gold:'/static/store/hj.png', //黄金
		diamond:'/static/store/bj.png', //钻石
		starshine:'/static/store/xy.png', //星耀
		honor:'/static/store/wz.png', //王者
	}
	
	const kefuShow = ref(0);
	
	
	const giveaway = {
		'maker': '/storage/myicon/20250324/22514d82b240593051f59f66a9a48370.png', //BD   maker
		'leader': '/storage/myicon/20250324/ef0036f44b822375f6c535157e6f92c5.png', //BDM  leader
		'short_partner': '/storage/myicon/20250324/94d8c29e55576422e5231193e8225c5e.png', //服务中心  short_partner
		'high_partner': '/storage/myicon/20250324/9e1848cbfc03f8cdf38456e698dea687.png', //区县  high_partner
		'founder': '/storage/myicon/20250324/39e5b0a6bcc9fb0c726702d663f50753.png', //地市  founder
		'shareholder':'/static/user/stockholder.png',//股东
	}
	//计算股东的另外一个徽章
	const calcSrc = computed(()=>{
		let role = '';
		if(userInfo.value?.extend?.founder==1){
			role = 'founder'
		}else if(userInfo.value?.extend?.high_partner==1){
			role = 'high_partner'
		}else if(userInfo.value?.extend?.short_partner==1){
			role = 'high_partner';
		}else if(userInfo.value?.extend?.leader==1){
			role = 'leader'
		}else if(userInfo.value?.extend?.maker==1){
			role = 'maker'
		}
		return sheep.$url.cdn(giveaway[role]);
	})
	
	const isAuditMode = computed(() => sheep.$store('app').isAuditMode);

	// 用户信息
	const userInfo = computed(() => sheep.$store('user').userInfo);
	// 是否登录
	const isLogin = computed(() => sheep.$store('user').isLogin);

	const city_name = ref(null)
	// 接收参数
	const props = defineProps({
		background: {
			type: String,
			default: '',
		},
		// 头像
		avatar: {
			type: String,
			default: '',
		},
		nickname: {
			type: String,
			default: '请先登录',
		},
		vip: {
			type: [String, Number],
			default: '1',
		},
		collectNum: {
			type: [String, Number],
			default: '1',
		},
		likeNum: {
			type: [String, Number],
			default: '1',
		},
	});
	const DEFAULT_LOCATION = {
		latitude: 30.308041, // 默认坐标
		longitude: 120.372893,
		city_name: "杭州市",
	};

	function onBind() {
		showAuthModal('changeMobile');
	}
	const getLocation = () => {
		city_name.value = '定位中'
		uni.getLocation({
			type: "wgs84",
			success(res) {
				const {
					latitude,
					longitude
				} = res;
				getCityName(latitude, longitude);
			},
			fail(err) {
				console.error("定位失败:", err);
				// 定位失败时使用默认坐标
				getCityName(DEFAULT_LOCATION.latitude, DEFAULT_LOCATION.longitude);
				uni.showToast({
					title: "定位失败",
					icon: "none",
					duration: 2000,
				});
			},
		});
	};
	const getCityName = (latitude, longitude) => {
		sheep.$api.data
			.getcity({
				latitude: latitude,
				longitude: longitude,
			})
			.then((res) => {
				if (res.error === 0) {
					city_name.value = res.data.name
				}
			});
	};
	const sign = async () => {
		if (!userInfo?.is_signin) {
			if (isLogin.value) {
				const {error,data} = await sheep.$api.point.signUp()
				userInfo.value.is_signin = true
				// uni.showToast({
				// 	title: '请等待开放',
				// 	icon: 'none'
				// })
			} else {
				showAuthModal()
			}
		}
	}
	async function getOwnStoreInfo(){
		if(isLogin.value){
			let {error,data} = await sheep.$api.store.getOwnStoreInfo()
			if(error == 0){
				grade.value = data.grade
			}
		}
	}
	function init() {
		// if (uni.getStorageSync('location')) {
		// 		  if (JSON.parse(uni.getStorageSync('location'))?.city_name) {
		// 		  		city_name.value=  JSON.parse(uni.getStorageSync('location'))?.city_name
		// 				return
		// 		  }
		//   return
		// }
		// getLocation()

	}
	function toLogin(){
		if(isLogin.value){
			sheep.$router.go('/pages/user/setting')
		}else{
			showAuthModal()
		}
	}
	function customerClick() {
		showKefuModal()
	}

	async function getCfg(){
		const res = await sheep.$api.data.getCfg({code:'basic.site.kefu_display'});
		console.log('getCfg.res:',res)
		if(res.error==0){
			kefuShow.value = res.data||0;
		}
	}

	onLoad(() => {
		getOwnStoreInfo()
		init();
		getCfg();

	})
</script>

<style lang="scss" scoped>
	.ss-user-info-wrap {
		box-sizing: border-box;
		width: 750rpx;
		max-width: 750rpx;
		.avatar-box {
			width: 152rpx;
			height: 152rpx;
			border-radius: 50%;
			overflow: hidden;
			flex-shrink: 0;

			.avatar-img {
				width: 152rpx;
				min-width: 152rpx;
				height: 152rpx;
			}
		}

		.nick-name {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
			line-height: normal;
			flex-shrink: 0;
			margin-right: 8rpx;
		}

		.vip-img {
			width: 30rpx;
			height: 30rpx;
		}

		.sicon-qrcode {
			font-size: 40rpx;
		}
	}
	.open-vip{
		margin: 10rpx 20rpx;
		.open-vip-image{
			width: 710rpx;
			height: 160rpx;
		}
	}
	.bind-mobile-box {
		width: 100%;
		height: 84rpx;
		padding: 0 34rpx 0 44rpx;
		box-sizing: border-box;
		background: #ffffff;
		box-shadow: 0px -8rpx 9rpx 0px rgba(#e0e0e0, 0.3);

		.cicon-mobile-o {
			font-size: 30rpx;
			color: #ff690d;
		}

		.mobile-title {
			font-size: 24rpx;
			font-weight: 500;
			color: #ff690d;
		}

		.bind-btn {
			width: 100rpx;
			height: 50rpx;
			background: #ff6100;
			border-radius: 25rpx;
			font-size: 24rpx;
			font-weight: 500;
			color: #ffffff;
		}
	}

	.tagSore-box {
		position: relative;
		margin-left: 20rpx;

		.tagSore-img {
			position: absolute;
			width: 50rpx;
			height: 50rpx;
			left: -25rpx;
			top: -10rpx;
		}

		.tagSore-item {
			width: 108rpx;
			height: 30rpx;
			background: linear-gradient(270deg, #B50606 15%, #FC6969 68%);
			text-align: center;
			color: #fff;
			font-size: 22rpx;
			border-radius: 6rpx;


		}
	}

	.gray-cover {
		-webkit-filter: grayscale(100%);
		-moz-filter: grayscale(100%);
		-ms-filter: grayscale(100%);
		-o-filter: grayscale(100%);
		filter: grayscale(100%);
		filter: gray;

	}

	.task-box {
		width: 710rpx;
		height: 128rpx;
		border-radius: 20rpx;
		background: #FFFFFF;
		margin: 0 auto;
		// background-image: url('https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/mengchong/task-bg2.png');
		// background-size: 100% 100%;
		box-sizing: border-box;

		.left {
			color: var(--ui-BG-5);

			.gold {
				.num {
					font-family: PingFang SC;
					font-size: 36rpx;
					font-weight: normal;
					line-height: normal;
					text-align: center;
				}

				.text {
					font-family: PingFang SC;
					font-size: 24rpx;
					font-weight: normal;
					line-height: normal;
					text-align: center;
				}
			}

			.line {
				width: 2rpx;
				height: 60rpx;
				border-left: 2rpx solid rgba(43, 110, 222, 0.8);
			}

			.points {
				color: var(--ui-BG-5);

				.num {
					font-family: PingFang SC;
					font-size: 36rpx;
					font-weight: normal;
					line-height: normal;
					text-align: center;
				}

				.text {

					view {
						font-size: 24rpx;

					}

					image {
						width: 12rpx;
						height: 18rpx;
					}
				}
			}
		}

		.right {
			position: relative;

			.sign-pic {
				width: 168rpx;
				height: 95rpx;
				position: absolute;
				right: -50rpx;
				top: -50rpx;
			}

			.ss-reset-button {
				width: 159rpx;
				height: 55rpx;
				border-radius: 10rpx;
				background: #2B6EDE;
				font-family: Source Han Sans;
				font-size: 28rpx;
				font-weight: normal;
				line-height: normal;
				letter-spacing: normal;
				color: #FFFFFF;
			}


		}
	}
	.income-img {
		width: 60rpx;
		height: 60rpx;
		margin-right:13rpx;
	}
	.huiyuan-img{
		width: 196rpx;
		height: 50rpx;
	}
	.po{
		position: relative;
	}
	.btn-groups{
		position: absolute;
		// right:-320rpx;
		right: 20rpx;
		bottom:0rpx;
	}


</style>